<?php if ($receipts): ?>
    <div id="Table_event" class="table-responsive">
        <table class="table table-bordered bootstrap-datatable event-list">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Date</th>
                    <th>Order Date</th>
                    <th>Account Code</th>
                    <th>Account Name</th>
                    <th>Company</th>
                    <th>Service Name</th>
                    <th>Price</th>
                    <th>GST</th>
                    <th>Discount</th>
                    <th>Cash</th>
                    <th>Paypal</th>
                    <th>Credit Card</th>
                    <th>NETS</th>
                    <th>Package sessions used</th>
                    <th>Package Revenue</th>
                    <th>Retail Revenue</th>
                    <th>Retail Discount</th>
                    <th>Person in Charge</th>
                    <th>Customer</th>
                    <th>Branch</th>
                </tr>
            </thead>  
            <tbody>
                <?php
                $total_price           = 0;
                $total_discount        = 0;
                $total_tax             = 0;
                $total_cash            = 0;
                $total_paypal          = 0;
                $total_credit          = 0;
                $total_package_session = 0;
                $total_package         = 0;
                $total_retail          = 0;
                $total_discount_retail = 0;
                ?>
                <?php foreach ($receipts as $i => $v): ?>
                    <tr>
                        <td><?php echo $i + 1 ?></td>
                        <td><?php echo date('d/M/y', @$v->order->appoint->date) ?></td>
                        <td><?php echo date('d/M/y', @$v->order->order_date) ?></td>
                        <td></td>
                        <td></td>

                        <!--Root Merchant-->
                        <td>
                            <?php if (@$v->order->merchant): ?>
                                <?php echo @$v->order->merchant->isBranch ? $v->order->merchant->author->name : $v->order->merchant->name; ?>
                            <?php else: ?>
                                <?php echo @$v->order->service->user->name; ?>
                                <!--End Root Merchant-->
                            <?php endif; ?>
                        </td>

                        <td><?php echo @$v->order->service->s_name ?></td>
                        <td>$<?php echo @$v->order->s_price ?></td>

                        <!--Tax-->
                        <td>$<?php
                            echo $v->tax . ' (' . @$v->order->tax . '%)';
                            ?>
                        </td>
                        <!--End Tax-->

                        <!--Discount-->
                        <td>$<?php
                            $discount = @$v->discount_type == '$' ? $v->discount : @$v->order->s_price * $v->discount / 100;
                            echo $discount
                            ?>
                        </td>
                        <!--Discount-->

                        <td><?php echo @$v->order->payment_method == Orders::PAYMENT_CASH ? '$' . @$v->final_price : '' ?></td>
                        <td><?php echo @$v->order->payment_method == Orders::PAYMENT_PAYPAL ? '$' . @$v->final_price : '' ?></td>
                        <td><?php echo @$v->order->payment_method == Orders::PAYMENT_CREDIT ? '$' . @$v->final_price : '' ?></td>
                        <td></td>
                        <td><?php echo @$v->order->packageOrder ? '1' : '' ?></td>

                        <!--Package Revenue-->
                        <td><?php
                            if (@$v->order->packageOrder)
                            {
                                $pack         = $v->order->packageOrder->packageCustomer->package;
                                $pack_revenue = $pack->price / $pack->limit;
                                echo '$' . $pack_revenue;
                            }
                            else
                            {
                                echo '';
                            }
                            ?>
                        </td>
                        <!--End Package Revenue-->

                        <!--Retail Revenue-->
                        <td><?php
                            $retail_receipt_price    = 0;
                            $retail_receipt_discount = 0;
                            if (@$v->order->retailOrder && $v->order->retailOrder->receipt)
                            {
                                $retail_receipt_price    = $v->order->retailOrder->receipt->final_price;
                                $retail_receipt_discount = $v->order->retailOrder->receipt->discount_type == '%' ?
                                        $v->order->retailOrder->total_price * $v->order->retailOrder->receipt->discount :
                                        $v->order->retailOrder->receipt->discount;
                            }
                            echo $retail_receipt_price != 0 ? '$' . $retail_receipt_price : '';
                            ?>
                        </td>
                        <!--End Retail Revenue-->

                        <!--Retail Discount-->
                        <td>
                            <?php echo $retail_receipt_discount != 0 ? '$' : $retail_receipt_discount ?>
                        </td>
                        <!--End Retail Discount-->

                        <td><?php echo @$v->order->staffsPerform ?></td>
                        <td><?php echo @$v->order->customer->name ?></td>

                        <!--Branch-->
                        <td><?php echo @$v->order->merchant->isBranch ? $v->order->merchant->name : ''; ?></td>
                        <!--End Branch-->
                    </tr>
                    <!--Count Total-->
                    <?php
                    $total_price+=@$v->order->s_price;
                    $total_discount+=$discount;
                    $total_tax+=@$v->tax;
                    $total_cash += @$v->order->payment_method == Orders::PAYMENT_CASH ? @$v->order->service->price + @$gst : 0;
                    $total_paypal += @$v->order->payment_method == Orders::PAYMENT_PAYPAL ? @$v->order->service->price + @$gst : 0;
                    $total_credit += @$v->order->payment_method == Orders::PAYMENT_CREDIT ? @$v->order->service->price + @$gst : 0;
                    $total_package_session += @$v->order->packageOrder ? 1 : 0;
                    $total_package += @$v->order->packageOrder ? $pack_revenue : 0;
                    $total_retail += $retail_receipt_price;
                    $total_discount_retail += $retail_receipt_discount;
                    ?>
                    <!--End Count Total-->
                <?php endforeach; ?>
                <tr class="total">
                    <td colspan="7">
                        Total:
                    </td>
                    <td>
                        $<?php echo $total_price; ?>
                    </td>
                    <td>
                        $<?php echo $total_tax; ?>
                    </td>
                    <td>
                        $<?php echo $total_discount; ?>
                    </td>
                    <td>
                        $<?php echo $total_cash; ?>
                    </td>
                    <td>
                        $<?php echo $total_paypal; ?>
                    </td>
                    <td colspan="2">
                        $<?php echo $total_credit; ?>
                    </td>
                    <td>
                        $<?php echo $total_package; ?>
                    </td>
                    <td>
                        <?php echo $total_package_session; ?>
                    </td>
                    <td>
                        $<?php echo $total_retail; ?>
                    </td>
                    <td colspan="4">
                        $<?php echo $total_discount_retail; ?>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
<?php endif; ?>
<?php if ($pages): ?>
    <div class="pagination pagination-centered">
        <?php
        $this->widget('application.widgets.MyLinkPager', array(
            'pages'                => $pages,
            'header'               => '',
            'maxButtonCount'       => 5,
            'nextPageLabel'        => '&raquo;',
            'firstPageLabel'       => '←',
            'lastPageLabel'        => '→',
            'prevPageLabel'        => '&laquo;',
            'selectedPageCssClass' => 'active',
        ))
        ?>
    </div>
<?php endif; ?>